#!/usr/bin/env python
# encoding: utf-8
"""
forum.py

Created by 姚 远 on 2009-07-08.
Copyright (c) 2009 Knarfytrebil. All rights reserved.
"""
import wsgiref.handlers
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp \
	import template

class topic(db.Model):
	ttl=db.StringProperty()
	when = db.DateTimeProperty(auto_now_add=True)
	who=db.StringProperty()
	cnts=db.TextProperty()
	gd=db.IntegerProperty()
	bd=db.IntegerProperty()


class MainHandler(webapp.RequestHandler):	
	def get(self):
		user = users.get_current_user()
		tpics = db.GqlQuery('SELECT * FROM topic '
		'ORDER BY when DESC')
		values = {
			'user':str(user),
			'tpics':tpics
		}
		self.response.out.write(template.render('Forum.html', values))
	def post(self):
		usr=user = users.get_current_user()
		tpic = topic(ttl=self.request.get('ttl'),who=str(usr),cnts=self.request.get('cnts'))
		tpic.put()
		self.redirect('/Forum.html')


def main():
	  forum = webapp.WSGIApplication([('/Forum\.html', MainHandler)],
	                                       debug=True)
	  wsgiref.handlers.CGIHandler().run(forum)

if __name__ == '__main__':
	main()

